package com.yidusoft.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidusoft.core.Result;
import com.yidusoft.core.ResultGenerator;
import com.yidusoft.domain.SecMenu;
import com.yidusoft.service.SecMenuService;
import com.yidusoft.shiro.ShiroService;
import com.yidusoft.utils.CodeHelper;
import com.yidusoft.utils.TreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
* Created by CodeGenerator on 2017/07/18.
*/
@Controller
@RequestMapping("/sec/menu")
public class SecMenuController {
    @Resource
    private SecMenuService secMenuService;
    @Resource
    private ShiroService shiroService;


    @RequestMapping(value ="/linkAdd")
    public String linkAdd(String id,Model model){
        List<Integer> sorts = secMenuService.querySort(id);
        model.addAttribute("sorts",sorts);
        model.addAttribute("currentSort",sorts.size()+1);
        model.addAttribute("parentId",id);
        return  "secmenu/secmenu-add";
    }

    @RequestMapping(value ="/linkList")
    public String linkList(){
        return  "secmenu/secmenu-list";
    }

    @RequestMapping(value ="/linkUpdate")
    public String linkUpdate(Model model,String id){
        SecMenu secMenu = secMenuService.findById(id);
        List<Integer> sorts = secMenuService.querySort(secMenu.getParentId());
        model.addAttribute("secMenu",secMenu);
        model.addAttribute("sorts",sorts);
        return  "secmenu/secmenu-update";
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(SecMenu secMenu) {
        if (secMenu.getUrl()==null||secMenu.getUrl()==""){
            secMenu.setUrl("/");
        }
        String parentId = secMenu.getParentId();
        secMenu.setId(UUID.randomUUID().toString());
        secMenu.setMenuCode(CodeHelper.getCode("CD"));
        secMenu.setCreateTime(new Date());
        System.out.println("prantId:"+secMenu.getParentId());
        secMenuService.addSort(parentId,secMenu.getSort());
        secMenuService.save(secMenu);
        //更新权限
        shiroService.updatePermission();
        return ResultGenerator.genSuccessResult();
    }

   //把数据永久删除
    @PostMapping("/deleteTrue")
    @ResponseBody
    public Result deleteTrue(String  id) {
        secMenuService.deleteById(id);
        //更新权限
        shiroService.updatePermission();
        return ResultGenerator.genSuccessResult();
    }

    //逻辑删除，数据还在
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(String  id) {
        SecMenu secMenu = secMenuService.findById(id);
        secMenu.setDeleted(1);
        secMenuService.deleteSort(id);
        secMenuService.update(secMenu);
        //更新权限
        shiroService.updatePermission();
        return ResultGenerator.genSuccessResult();
    }


    //逻辑删除，数据还在 批量删除
    @PostMapping("/deleteBacth")
    @ResponseBody
    public Result deleteBacth(String  ids) {
        String arr [] = ids.split(",");
        for(String str : arr){
            secMenuService.deleteSort(str);
            List<String> list = new ArrayList<>();
            list=secMenuService.getChildrenById(str);
            for(String  id: list){
                secMenuService.deleteSort(str);
            }
        }
        for(String str : arr){
            SecMenu secMenu = secMenuService.findById(str);
            secMenu.setDeleted(1);
            List<String> list = new ArrayList<>();
            list=secMenuService.getChildrenById(str);
            for(String  id: list){
                SecMenu Menu = secMenuService.findById(id);
                Menu.setDeleted(1);
                secMenuService.update(Menu);
            }
            secMenuService.update(secMenu);
            //更新权限
            shiroService.updatePermission();
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(SecMenu secMenu) {
        if (secMenu.getUrl()==null||secMenu.getUrl()==""){
            secMenu.setUrl("/");
        }
        secMenuService.updateSort(secMenu.getId(),secMenu.getSort());
        secMenuService.update(secMenu);
        //更新权限
        shiroService.updatePermission();
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ResponseBody
    public Result detail(@PathVariable String id) {
        SecMenu secMenu = secMenuService.findById(id);
        return ResultGenerator.genSuccessResult(secMenu);
    }


    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list(SecMenu secMenu) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SecMenu> list = new ArrayList<>();
        if(secMenu!=null){
            list = secMenuService.getMenu(secMenu);
        } else {
            list = secMenuService.queryAll();
        }
        map.put("Rows",list);
        map.put("Total",list.size());
        return map;
    }


    @PostMapping("/listByPage")
    @ResponseBody
    public Result listByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SecMenu> list = secMenuService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/menuTree")
    @ResponseBody
    public List<TreeNode> menuTree() {
        return   secMenuService.menuTree();
    }

    @PostMapping("/menuToTree")
    @ResponseBody
    public List<SecMenu> menuToTree(String id) {
        List<SecMenu>  menu=secMenuService.menuToTree(id);
        return   menu;
    }
    //查询可用 没被删除的菜单
    @RequestMapping(value={"/getMenuForAvailable",""})
    public String getMenuForAvailable(Model model){
      /*  List<SecMenu> list = secMenuService.getMenuForAvailable();
        model.addAttribute("menu",list);
        model.addAttribute("m",list);*/
        return  "index";
    }

    @PostMapping("/getChildrenById")
    @ResponseBody
    public List<String> getChildrenById(String ids){
        String arr [] = ids.split(",");
        List<String> list = new ArrayList<>();
        for(String id : arr) {
            System.out.println(secMenuService.getChildrenById(id));
            list=secMenuService.getChildrenById(id);
        }
        return list;
    }
}
